var api = require('../../../config/api.js');
var util = require('../../../utils/util.js');
var user = require('../../../utils/user.js'); 
var app = getApp();
Page({
  data: {
    show: true,
    mobile:"2"
  },
  onChange(event) {
    // event.detail 为当前输入的值
    console.log(event.detail);
    this.setData({ mobile: event.detail });
  },
  // 绑定手机号
  bindPhoneNumber: function(e) {
    console.log('bindPhoneNumber')
    console.log(e)

    const userInfo=wx.getStorageSync('userInfo')

    if (!userInfo ) {
      wx.showToast({
        title: '绑定失败：请先登录',
        icon: 'none',
        duration: 2000
      });
      // 拒绝授权
      return;
    }

    /* console.log('this.data.hasLogin')
    console.log(this.data.hasLogin)
    if (!this.data.hasLogin) {
      wx.showToast({
        title: '绑定失败：请先登录',
        icon: 'none',
        duration: 2000
      });
      return;
    } */

    console.log('AuthBindPhone request')
    console.log({
      userId:userInfo.userId,
      iv: e.detail.iv,
      encryptedData: e.detail.encryptedData
    })
    // debugger
    util.request(api.AuthBindPhone, {
      userId:userInfo.userId,
      iv: e.detail.iv,
      encryptedData: e.detail.encryptedData
    }, 'POST').then(function(res) {
      console.log('bindPhoneNumber res ok')
       
      if (res.errno === 0) {
        wx.showToast({
          title: '绑定手机号码成功',
          icon: 'success',
          duration: 2000
        });
        // 登录完成
        app.globalData.hasLogin = true;     
         
          wx.navigateTo({
            url: "/pages/dev/dev"
          });
      }else{
        wx.showModal({
          title: '提示',
          content:res.errmsg,
          showCancel:false
        });
        return false
      }
    });
  },
  onClose() {
    this.setData({ close: false });
  },
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    // 页面渲染完成
    this.wxLogin()

  },
  onReady: function() {

  },
  onShow: function() {
    // 页面显示
  },
  onHide: function() {
    // 页面隐藏

  },
  onUnload: function() {
    // 页面关闭

  },
  wxLogin: function(e) {
    console.log('fasfa')
    console.log(e)
    /* if (e.detail.userInfo == undefined) {
      
        console.log('e.detail.userInfo == undefined')
      app.globalData.hasLogin = false;
      util.showErrorToast('微信登录失败');
      return;
    } */
    /* wx.showToast({
      title: '微信登录'
      }) */
      var that=this
   
      user.loginByWeixin().then(res => {
      //  跳过手机绑定
        /* if(res.data.userInfo.mobile.length<11){
          console.log('res.data.userInfo.mobile.length<11')
          that.bindPhoneNumber(e,res.data.userInfo.userId)
        }else{ */
          // app.globalData.hasLogin = true;   
            
          console.log('loginByWeixin - ok')
          
          /* wx.navigateBack({
            delta: 1
          }) */
        // }       
      }).catch((err) => {
        app.globalData.hasLogin = false;
        util.showErrorToast('微信登录失败~');
      });
      console.log('zzzzzz')

  },
  accountLogin: function() {
    wx.navigateTo({
      url: "/pages/auth/accountLogin/accountLogin"
    });
  }
})